#encoding: utf-8
class ApplicationController < ActionController::Base

  before_filter :set_locale, :make_mailer_use_request_host

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

  #use current request for ActionMailer
  def make_mailer_use_request_host
    ActionMailer::Base.default_url_options[:host] = request.host_with_port
  end

  def login_required
    if session[:student]
      return true
    end
    flash[:alert] = t "general.login.logintocontinue"
    session[:return_to]=request.fullpath
    redirect_to :root
  end

  def current_student
    session[:student]
  end

  def redirect_to_stored
    if (return_to = session[:return_to])
      session[:return_to]=nil
      redirect_to return_to
    else
      redirect_to :controller => 'frontend/portal', :action => 'index'
    end
  end

  def redirect_to_home
    redirect_to :controller => "frontend/portal", :action => "index"
    #redirect_to :root
  end

  def check_admin_priviledge
    if session[:student]
      #check admin priviledge
      group_links = StudentGroupLink.check_admin(current_student[:id])
      if group_links
        @is_admin = 1
        return true
      end
    end
    flash[:alert] = t "general.login.accessdenied"
    session[:return_to]=request.fullpath
    redirect_to url_for(:controller => 'frontend/portal', :action => 'access_denied')
  end

  def admin_log(content,type='')
    DashboardLog.log(content,type)
  end

  def sort_column(model, col='title')
    model.column_names.include?(params[:sort]) ? params[:sort] : col
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end

  def format_status(status)
    status_array = {
        '0' => t('library.document.not_available'),
        '1' => t('library.document.available'),
        '-1' => t('library.document.deleted'),
        ''  =>  t('library.document.unknown')
    }
    @status_out = status_array[status]
  end

end
